﻿//Test by Bangle
using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using Bangle.DBUtility;
namespace Bangle.DAL  
{
	 	//T_SaleDetail
		public partial class T_SaleDetailDAL
	{
   		     
		public bool Exists(Guid SaleDetailId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_SaleDetail");
			strSql.Append(" where ");
			                                       strSql.Append(" SaleDetailId = @SaleDetailId  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@SaleDetailId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleDetailId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Bangle.Model.T_SaleDetail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_SaleDetail(");			
            strSql.Append("SaleDetailId,SaleId,PurchaseTime,PurchaseCode,PurchaseReceipt,PurchasePrice,PurchaseAmount,Quantity,SupplierId,SupplierName,SalePriceUSD,SalePriceRMB,SaleAmountUSD,SaleAmountRMB,CreateUserId,CreateTime,UpdateUserId,UpdateTime,Valid");
			strSql.Append(") values (");
            strSql.Append("@SaleDetailId,@SaleId,@PurchaseTime,@PurchaseCode,@PurchaseReceipt,@PurchasePrice,@PurchaseAmount,@Quantity,@SupplierId,@SupplierName,@SalePriceUSD,@SalePriceRMB,@SaleAmountUSD,@SaleAmountRMB,@CreateUserId,@CreateTime,@UpdateUserId,@UpdateTime,@Valid");            
            strSql.Append(") ");            
            		
			SqlParameter[] parameters = {
			            new SqlParameter("@SaleDetailId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@PurchaseTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@PurchaseCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@PurchaseReceipt", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@PurchasePrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PurchaseAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Quantity", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SupplierName", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@SalePriceUSD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SalePriceRMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SaleAmountUSD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SaleAmountRMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.SaleDetailId;                        
            parameters[1].Value = model.SaleId;                        
            parameters[2].Value = model.PurchaseTime;                        
            parameters[3].Value = model.PurchaseCode;                        
            parameters[4].Value = model.PurchaseReceipt;                        
            parameters[5].Value = model.PurchasePrice;                        
            parameters[6].Value = model.PurchaseAmount;                        
            parameters[7].Value = model.Quantity;                        
            parameters[8].Value = model.SupplierId;                        
            parameters[9].Value = model.SupplierName;                        
            parameters[10].Value = model.SalePriceUSD;                        
            parameters[11].Value = model.SalePriceRMB;                        
            parameters[12].Value = model.SaleAmountUSD;                        
            parameters[13].Value = model.SaleAmountRMB;                        
            parameters[14].Value = model.CreateUserId;                        
            parameters[15].Value = model.CreateTime;                        
            parameters[16].Value = model.UpdateUserId;                        
            parameters[17].Value = model.UpdateTime;                        
            parameters[18].Value = model.Valid;                        
			            DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Bangle.Model.T_SaleDetail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_SaleDetail set ");
			                        
            strSql.Append(" SaleDetailId = @SaleDetailId , ");                                    
            strSql.Append(" SaleId = @SaleId , ");                                    
            strSql.Append(" PurchaseTime = @PurchaseTime , ");                                    
            strSql.Append(" PurchaseCode = @PurchaseCode , ");                                    
            strSql.Append(" PurchaseReceipt = @PurchaseReceipt , ");                                    
            strSql.Append(" PurchasePrice = @PurchasePrice , ");                                    
            strSql.Append(" PurchaseAmount = @PurchaseAmount , ");                                    
            strSql.Append(" Quantity = @Quantity , ");                                    
            strSql.Append(" SupplierId = @SupplierId , ");                                    
            strSql.Append(" SupplierName = @SupplierName , ");                                    
            strSql.Append(" SalePriceUSD = @SalePriceUSD , ");                                    
            strSql.Append(" SalePriceRMB = @SalePriceRMB , ");                                    
            strSql.Append(" SaleAmountUSD = @SaleAmountUSD , ");                                    
            strSql.Append(" SaleAmountRMB = @SaleAmountRMB , ");                                    
            strSql.Append(" CreateUserId = @CreateUserId , ");                                    
            strSql.Append(" CreateTime = @CreateTime , ");                                    
            strSql.Append(" UpdateUserId = @UpdateUserId , ");                                    
            strSql.Append(" UpdateTime = @UpdateTime , ");                                    
            strSql.Append(" Valid = @Valid  ");            			
			strSql.Append(" where SaleDetailId=@SaleDetailId  ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@SaleDetailId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@PurchaseTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@PurchaseCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@PurchaseReceipt", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@PurchasePrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PurchaseAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Quantity", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SupplierName", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@SalePriceUSD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SalePriceRMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SaleAmountUSD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@SaleAmountRMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.SaleDetailId;                        
            parameters[1].Value = model.SaleId;                        
            parameters[2].Value = model.PurchaseTime;                        
            parameters[3].Value = model.PurchaseCode;                        
            parameters[4].Value = model.PurchaseReceipt;                        
            parameters[5].Value = model.PurchasePrice;                        
            parameters[6].Value = model.PurchaseAmount;                        
            parameters[7].Value = model.Quantity;                        
            parameters[8].Value = model.SupplierId;                        
            parameters[9].Value = model.SupplierName;                        
            parameters[10].Value = model.SalePriceUSD;                        
            parameters[11].Value = model.SalePriceRMB;                        
            parameters[12].Value = model.SaleAmountUSD;                        
            parameters[13].Value = model.SaleAmountRMB;                        
            parameters[14].Value = model.CreateUserId;                        
            parameters[15].Value = model.CreateTime;                        
            parameters[16].Value = model.UpdateUserId;                        
            parameters[17].Value = model.UpdateTime;                        
            parameters[18].Value = model.Valid;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid SaleDetailId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_SaleDetail ");
			strSql.Append(" where SaleDetailId=@SaleDetailId ");
						SqlParameter[] parameters = {
					new SqlParameter("@SaleDetailId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleDetailId;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Bangle.Model.T_SaleDetail GetModel(Guid SaleDetailId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SaleDetailId, SaleId, PurchaseTime, PurchaseCode, PurchaseReceipt, PurchasePrice, PurchaseAmount, Quantity, SupplierId, SupplierName, SalePriceUSD, SalePriceRMB, SaleAmountUSD, SaleAmountRMB, CreateUserId, CreateTime, UpdateUserId, UpdateTime, Valid  ");			
			strSql.Append("  from T_SaleDetail ");
			strSql.Append(" where SaleDetailId=@SaleDetailId ");
						SqlParameter[] parameters = {
					new SqlParameter("@SaleDetailId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleDetailId;

			
			Bangle.Model.T_SaleDetail model=new Bangle.Model.T_SaleDetail();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
																								if(ds.Tables[0].Rows[0]["SaleDetailId"].ToString()!="")
				{
					model.SaleDetailId= Guid.Parse(ds.Tables[0].Rows[0]["SaleDetailId"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["SaleId"].ToString()!="")
				{
					model.SaleId= Guid.Parse(ds.Tables[0].Rows[0]["SaleId"].ToString());
				}
																								model.PurchaseTime= ds.Tables[0].Rows[0]["PurchaseTime"].ToString();
																																model.PurchaseCode= ds.Tables[0].Rows[0]["PurchaseCode"].ToString();
																																model.PurchaseReceipt= ds.Tables[0].Rows[0]["PurchaseReceipt"].ToString();
																												if(ds.Tables[0].Rows[0]["PurchasePrice"].ToString()!="")
				{
					model.PurchasePrice=decimal.Parse(ds.Tables[0].Rows[0]["PurchasePrice"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["PurchaseAmount"].ToString()!="")
				{
					model.PurchaseAmount=decimal.Parse(ds.Tables[0].Rows[0]["PurchaseAmount"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["Quantity"].ToString()!="")
				{
					model.Quantity=decimal.Parse(ds.Tables[0].Rows[0]["Quantity"].ToString());
				}
																																												if(ds.Tables[0].Rows[0]["SupplierId"].ToString()!="")
				{
					model.SupplierId= Guid.Parse(ds.Tables[0].Rows[0]["SupplierId"].ToString());
				}
																								model.SupplierName= ds.Tables[0].Rows[0]["SupplierName"].ToString();
																												if(ds.Tables[0].Rows[0]["SalePriceUSD"].ToString()!="")
				{
					model.SalePriceUSD=decimal.Parse(ds.Tables[0].Rows[0]["SalePriceUSD"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["SalePriceRMB"].ToString()!="")
				{
					model.SalePriceRMB=decimal.Parse(ds.Tables[0].Rows[0]["SalePriceRMB"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["SaleAmountUSD"].ToString()!="")
				{
					model.SaleAmountUSD=decimal.Parse(ds.Tables[0].Rows[0]["SaleAmountUSD"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["SaleAmountRMB"].ToString()!="")
				{
					model.SaleAmountRMB=decimal.Parse(ds.Tables[0].Rows[0]["SaleAmountRMB"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["CreateUserId"].ToString()!="")
				{
					model.CreateUserId=int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
				}
																																				model.CreateTime= ds.Tables[0].Rows[0]["CreateTime"].ToString();
																												if(ds.Tables[0].Rows[0]["UpdateUserId"].ToString()!="")
				{
					model.UpdateUserId=int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
				}
																																				model.UpdateTime= ds.Tables[0].Rows[0]["UpdateTime"].ToString();
																												if(ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM T_SaleDetail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM T_SaleDetail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
	}
}

